import Taro from "@tarojs/taro"
// import { pageToLogin } from "./utils.js"
import {HTTP_STATUS} from './config'
import {Options} from "@/network/type";
import {transmitUrlToVisit} from './proxy'
import MyUtils from "../pages/noticeApp/components/personal/utils";

const getEnv = () => {
   return MyUtils.getStorage("env") || MyUtils.getQueryVariable("env")
}

const transformProdUrl = (url) => {//打包环境下做检测
   let BASE_URL = ''
      , ENV_TYPE = Taro.getEnv()
   switch (ENV_TYPE) {
      case Taro.ENV_TYPE.WEAPP :
         BASE_URL = transmitUrlToVisit(url);
         break;//微信小程序环境
      case Taro.ENV_TYPE.SWAN :
         BASE_URL = url;
         break;//百度小程序环境
      case Taro.ENV_TYPE.ALIPAY :
         BASE_URL = url;
         break;//支付宝小程序环境
      case Taro.ENV_TYPE.TT :
         BASE_URL = url;
         break;//字节跳动小程序环境
      case Taro.ENV_TYPE.WEB :
         BASE_URL = url;
         break;//WEB(H5)环境
      case Taro.ENV_TYPE.RN :
         BASE_URL = url;
         break;//ReactNative 环境
      case Taro.ENV_TYPE.QQ :
         BASE_URL = url;
         break;//QQ小程序 环境
      case Taro.ENV_TYPE.JD :
         BASE_URL = url;
         break;//京东小程序 环境
      default:
         BASE_URL = url;
         break;//默认 环境
   }
   
   if (getEnv() === 'weapp') {//微信小程序嵌入H5的时候，需要代理
      BASE_URL = transmitUrlToVisit(url)
   }

   return BASE_URL
}

const handCustomUrlofParams = (requestParams: Options) => {
   let params = requestParams
   params.url = params.autoProxy ? transformProdUrl(params.url) : params.url
   return params
}

const customInterceptor = (chain) => {
   const requestParams: Options = handCustomUrlofParams(chain.requestParams)
   requestParams.loading && Taro.showLoading({
      title: requestParams.loading === true ? '请稍后' : requestParams.loading,
   })
   return chain.proceed(requestParams).then(res => {
      requestParams.loading && Taro.hideLoading()
      // 只要请求成功，不管返回什么状态码，都走这个回调
      if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
         return Promise.reject({desc: '请求资源不存在'})

      } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
         return Promise.reject({desc: "服务端出现了问题"})

      } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
         Taro.setStorageSync("Authorization", "")
         //   pageToLogin()
         // TODO 根据自身业务修改
         return Promise.reject({desc: "没有权限访问"});

      } else if (res.statusCode === HTTP_STATUS.AUTHENTICATE) {
         Taro.setStorageSync("Authorization", "")
         //   pageToLogin()
         return Promise.reject({desc: "需要鉴权"})

      } else if (res.statusCode === HTTP_STATUS.SERVER_ERROR) {
         return Promise.reject({desc: "服务器错误"});
      } else if (res.statusCode === HTTP_STATUS.SUCCESS) {
         return Promise.resolve(res.data)
         //   if (res.data.code === 0) {
         //     return Promise.resolve(res.data)
         //   } else if (res.data.code == '-100') {
         //     //非法登录
         //     // pageToLogin()
         //   }
         //   else {
         //     return Promise.reject(res.data)
         //   }
      }
   }).catch(error => {
      requestParams.loading && Taro.hideLoading()
      console.error(error)
      return Promise.reject(error)
   })
}
// Taro 提供了两个内置拦截器
// logInterceptor - 用于打印请求的相关信息
// timeoutInterceptor - 在请求超时时抛出错误。
// const interceptors = [customInterceptor, Taro.interceptors.logInterceptor]
const interceptors = [customInterceptor]

export default interceptors
